package com.ruoyi.car.service.impl;

import java.util.Iterator;
import java.util.List;

import com.ruoyi.car.domain.CarInfo;
import com.ruoyi.car.domain.CarParkRecord;
import com.ruoyi.car.mapper.CarInfoMapper;
import com.ruoyi.car.service.ICarGarageService;
import com.ruoyi.car.service.ICarInfoService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.car.mapper.CarVehicleRecordMapper;
import com.ruoyi.car.domain.CarVehicleRecord;
import com.ruoyi.car.service.ICarVehicleRecordService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 用车记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-03
 */
@Service
public class CarVehicleRecordServiceImpl implements ICarVehicleRecordService {
    @Autowired
    private CarVehicleRecordMapper carVehicleRecordMapper;

    @Autowired
    private ICarInfoService carInfoService;

    @Autowired
    private ICarGarageService carGarageService;

    @Autowired
    private CarInfoMapper carInfoMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询用车记录
     *
     * @param vehicleRecordId 用车记录主键
     * @return 用车记录
     */
    @Override
    public CarVehicleRecord selectCarVehicleRecordByVehicleRecordId(Long vehicleRecordId) {
        return carVehicleRecordMapper.selectCarVehicleRecordByVehicleRecordId(vehicleRecordId);
    }

    /**
     * 查询用车记录列表
     *
     * @param carVehicleRecord 用车记录
     * @return 用车记录
     */
    @Override
    public List<CarVehicleRecord> selectCarVehicleRecordList(CarVehicleRecord carVehicleRecord) {

        List<CarVehicleRecord> list = carVehicleRecordMapper.selectCarVehicleRecordList(carVehicleRecord);
        CarVehicleRecord vo = null;
        Iterator<CarVehicleRecord> it = list.iterator();
        while (it.hasNext()) {
            vo = it.next();
            if (vo.getCarGarageId()!=null) {
                vo.setGarageName(carGarageService.selectCarGarageByGarageId(vo.getCarGarageId()).getGarageName());
            }
            vo.setVehicleName(userService.selectUserById(vo.getVehicleUser()).getUserName());

        }
        return list;
    }

    /**
     * 新增用车记录
     *
     * @param carVehicleRecord 用车记录
     * @return 结果
     */
    @Override
    public int insertCarVehicleRecord(CarVehicleRecord carVehicleRecord) {
        carVehicleRecord.setVehicleUser(SecurityUtils.getUserId());

        //验证车牌
        carInfoService.compare(carVehicleRecord.getCarNumber());
        carVehicleRecord.setCreateTime(DateUtils.getNowDate());
        return carVehicleRecordMapper.insertCarVehicleRecord(carVehicleRecord);
    }

    /**
     * 修改用车记录
     *
     * @param carVehicleRecord 用车记录
     * @return 结果
     */
    @Override
    public int updateCarVehicleRecord(CarVehicleRecord carVehicleRecord) {
        if ("1".equals(carVehicleRecord.getVehicleAuditStatus())) {
            CarInfo carInfo = carInfoMapper.selectCarInfoByCarNumber(carVehicleRecord.getCarNumber());

            carGarageService.reduceCarGarageNumber(Long.parseLong(carInfo.getColour()));

            carInfo.setType("2");

            carInfoService.updateCarInfo(carInfo);

        }

        if (StringUtils.isEmpty(String.valueOf(carVehicleRecordMapper.selectCarVehicleRecordByVehicleRecordId(carVehicleRecord.getVehicleRecordId()).getCarGarageId()))
                && StringUtils.isNotEmpty(String.valueOf(carVehicleRecord.getCarGarageId()))) {
            //修改车库车位数量
            carGarageService.editCarGarageNumber(carVehicleRecord.getCarGarageId());

            //修改汽车车库信息
            CarInfo carInfo = new CarInfo();
            carInfo.setCarNumber(carVehicleRecord.getCarNumber());

            carInfo.setColour(String.valueOf(carVehicleRecord.getCarGarageId()));
            carInfoService.updateCarInfoByCarNumber(carInfo);
        }


        carVehicleRecord.setUpdateTime(DateUtils.getNowDate());
        return carVehicleRecordMapper.updateCarVehicleRecord(carVehicleRecord);
    }

    /**
     * 批量删除用车记录
     *
     * @param vehicleRecordIds 需要删除的用车记录主键
     * @return 结果
     */
    @Override
    public int deleteCarVehicleRecordByVehicleRecordIds(Long[] vehicleRecordIds) {
        return carVehicleRecordMapper.deleteCarVehicleRecordByVehicleRecordIds(vehicleRecordIds);
    }

    /**
     * 删除用车记录信息
     *
     * @param vehicleRecordId 用车记录主键
     * @return 结果
     */
    @Override
    public int deleteCarVehicleRecordByVehicleRecordId(Long vehicleRecordId) {
        return carVehicleRecordMapper.deleteCarVehicleRecordByVehicleRecordId(vehicleRecordId);
    }
}
